package com.example.demo;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class DataController {
    @RequestMapping("getDate")
    public String getData(HttpServletRequest request) throws Exception{

//        String city[]="Klang%20Valley";
//        1	沙白安南县（Sabak Bernam）9 大港（Sungai Besar）12页
//        2 乌鲁雪兰莪县（Hulu Selangor）17 新古毛（Kuala Kubu Bharu）12
//        3	瓜拉雪兰莪县（Kuala Selangor）17	瓜拉雪兰莪（Kuala Selangor）17
//        4	鹅唛县（Gombak）17	士拉央新镇（Bandar Baru Selayang）17
//        5	巴生县（Klang）	巴生市（Klang）17
//        6	八打灵县（Petaling）17	梳邦（Subang）17
//        7	乌鲁冷岳县（Hulu Langat）17	万宜新镇（Bandar Baru Bangi）17
//        8	瓜拉冷岳县（Kuala Langat）17	直落拿督（Teluk Datok）1条数据
//        9	雪邦县（Sepang）17页 沙叻丁宜（Salak Tinggi）2条数据
        String [] arr =new String [18];
        arr[0]="Klang%20Valley";
        arr[1]="Sungai%20Besar"; arr[2]="Hulu%20Selangor"; arr[3]="Kuala%20Kubu%20Bharu"; arr[4]="Kuala%20Selangor";
        arr[5]="Kuala%20Selangor"; arr[6]="Gombak"; arr[7]="Bandar%20Baru%20Selayang"; arr[8]="Klang";
        arr[9]="Petaling";
        arr[10]="Subang"; arr[11]="Hulu%20Langat"; arr[12]="Bandar%20Baru%20Bangi"; arr[13]="Kuala%20Langat"; arr[14]="Teluk%20Datok";
        arr[15]="Sepang"; arr[16]="Salak%20Tinggi";
        arr[17]="Sabak%20Bernam";
        for (int i=0;i<arr.length;i++){
            new Thread(new Thread2(arr[i])).start();
            System.out.println(arr[i]+"============================城市线程启动！");
        }
        return "success";
    }

    public static String sendGet(String url) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url ;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段

            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                System.out.println("获取价格信息失败，正在重试============================!");
            }
        }
        return result;
    }

    public static Double getPrice(String url) {
       try{
           String priceString = sendGet(url);
           if(!"".equals(priceString)){
               JSONObject priceJson = JSONObject.fromObject(priceString);
               Double price = (Double) JSONObject.fromObject(JSONArray.fromObject(JSONArray.fromObject(JSONObject.fromObject(JSONArray.fromObject(priceJson.get("pdp_listing_booking_details")).get(0)).get("p3_display_rate")).get(0)).get(0)).get("amount");
               return price;
           }else{
               return 0D;
           }
       }catch (Exception e){
           throw e;
       }
    }

    class Thread2 implements Runnable{
        private String CityNma;

        public Thread2(String CityNma) {
            this.CityNma=CityNma;
        }

        @Override
        public void run() {
            //1首先循环城市
            //2循环页面（一共17页分页数据就是16  16*18=288）


            //获取基本信息的url
            String citystartUrl = "https://www.airbnb.cn/api/v2/explore_tabs?version=1.3.9&satori_version=1.0.12&_format=for_explore_search_web&experiences_per_grid=20&items_per_grid=18&guidebooks_per_grid=20&auto_ib=true&fetch_filters=true&has_zero_guest_treatment=true&is_guided_search=true&is_new_cards_experiment=true&luxury_pre_launch=false&query_understanding_enabled=false&show_groupings=true&supports_for_you_v3=true&timezone_offset=480&client_session_id=be21908c-8de3-465e-81fa-a61c477e6c4b&map_toggle=false&metadata_only=false&is_standard_search=true&refinement_paths%5B%5D=%2Fhomes&selected_tab_id=home_tab&allow_override%5B%5D=&s_tag=VvvL3IYt&section_offset=6&items_offset=";
            String cityurlpage = "&screen_size=large&query=";
            String cityendUrl="&_intents=p1&key=d306zoyjsyarp7ifhu67rjxn52tv0t20&currency=CNY&locale=zh";

            //获取价格url
            String priceUrl = "https://www.airbnb.cn/api/v2/pdp_listing_booking_details?force_boost_unc_priority_message_type=&guests=1&listing_id=";
            String endprice = "&show_smart_promotion=0&_format=for_web_dateless&_interaction_type=pageload&_intents=p3_book_it&_parent_request_uuid=2bc20fa4-9142-4e17-ac4f-43c6fafcaa8c&_p3_impression_id=p3_1544669380_w4uX3tycrqfTZnFq&number_of_adults=1&number_of_children=0&number_of_infants=0&key=d306zoyjsyarp7ifhu67rjxn52tv0t20&currency=CNY&locale=zh";


            ResponseEntity<String> response = null;

            HSSFWorkbook wb = new HSSFWorkbook();
            // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet sheet = wb.createSheet("airbnb");
            sheet.setDefaultColumnWidth(15); //统一设置列宽
            HSSFRow row = sheet.createRow(0);

            // 第四步，创建单元格，并设置值表头 设置表头居中
            HSSFCellStyle style = wb.createCellStyle();
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

            //声明列对象
            HSSFCell cell = null;

            //创建标题(字段)
            String titleValueList[] = {"可用设备", "设施详情","房间要求","卫生间数","床数", "房间类型", "最多住几个人", "评价总结", "id","赔偿","公共区域床位","名称","名称", "地址", "名称","有待确认","首页图片","主人介绍", "空", "房间类型","房间类型","评星","主人头像", "预定地址", "最后更新时间","空","最少入住夜晚数", "币种","是否出示证件","合作房东信息", "可获得权力","human","入住时间","退房时间","地址", "损坏赔偿","房间类型",  "国家编码","纬度","经度","地址", "无", "待确认","评论数","评论详情","评论数","可分享机制","价格","房间定位"};
            for (int k = 0; k < titleValueList.length; k++) {
                cell = row.createCell(k);
                cell.setCellValue(titleValueList[k]);
                cell.setCellStyle(style);
            }
            int lj = 1;


                for (int p= 0; p <18; p++){
                    System.out.println("现在循环到========================================================="+p+"页");
                    try {

                        System.out.println("现在循环到========================================================"+CityNma);
                        String s = sendGet(citystartUrl+p*18+cityurlpage+CityNma+cityendUrl);
                        String resultData = s;
                        JSONObject resultJson = JSONObject.fromObject(resultData);
                        JSONArray array = JSONArray.fromObject(resultJson.get("explore_tabs"));

                        JSONObject json2 = JSONObject.fromObject(array.get(0));
                        JSONArray sectionsArray = JSONArray.fromObject(json2.get("sections"));

                        JSONObject json3 = JSONObject.fromObject(sectionsArray.get(0));
                        JSONArray listingsArray = JSONArray.fromObject(json3.get("listings"));

                        for (int i = 0; i < listingsArray.size(); i++) {
                            Boolean getPriceFlag = true;
                            System.out.println("现在北京时间============================"+CityNma+CityNma+CityNma+CityNma+"============================"+new Date()+"第"+(p+1)+"页，第"+(i+1)+"个房间");
                            JSONObject single = JSONObject.fromObject(listingsArray.get(i));
                            JSONArray singlearray = JSONArray.fromObject(single.get("listing"));
                            //房间id
                            String id = ((JSONObject) singlearray.get(0)).get("id").toString();
                            String singleurl = "https://www.airbnb.cn/api/v2/pdp_listing_details/" + id + "?_format=for_rooms_show&key=d306zoyjsyarp7ifhu67rjxn52tv0t20&";
                            String singleInfo = sendGet(singleurl);
                            JSONObject singleJson = JSONObject.fromObject(singleInfo);
                            String sss = singleJson.get("pdp_listing_detail").toString();
                            JSONObject jasonObject = JSONObject.fromObject(sss);
                            Map map = (Map) jasonObject;
                            Set<String> keys = map.keySet();   //此行可省略，直接将map.keySet()写在for-each循环的条件中
                            List<String> list1 = new ArrayList<String>(keys);
                            LinkedList ldl = new LinkedList();

                            //获取价格
                            Double price = 0D;
                            while (getPriceFlag){
                                price  = getPrice(priceUrl+id+endprice);
                                if(price!=0D){
                                    getPriceFlag = false;
                                }
                            }
                            for (int z=1;z<list1.size()+1;z++){
                                if((z==1)||(z==2)||(z==3)||(z==4)||(z==5)||(z==6)||(z==7)||(z==8)||(z==9)||(z==10)||(z==11)||(z==12)||(z==13)||(z==14)||(z==15)||(z==16)||(z==17)||(z==18)||(z==19)||(z==20)||(z==21)||(z==23)||(z==23)||(z==25)||(z==26)||(z==27)||(z==29)||(z==30)||(z==31)||(z==33)||(z==34)||(z==38)||(z==39)||(z==40)||(z==41)||(z==42)||(z==43)||(z==44)||(z==46)||(z==49)||(z==50)||(z==51)||(z==57)||(z==60)||(z==66)||(z==67)||(z==68)||(z==82)){
                                    ldl.add(map.get(list1.get(z)));
                                }
                            }
                            ldl.add("￥"+price.toString());

                            ldl.add(CityNma+"第"+p+"页"+"第"+(i+1)+"个房间！");
                            row = sheet.createRow(lj);
                            for (int j = 0; j < ldl.size(); j++) {
                                //将内容按顺序赋给对应的列对象
                                if(j==0){
                                    row.createCell(j).setCellValue("有便利设施");
                                }else if(j==1){
                                    int titlesize = ((JSONArray) ldl.get(1)).size();
                                    String title = "设施详情：";
                                    for (int k=1;k<titlesize+1;k++){
                                        title=title+k+(((JSONObject) ((JSONArray) ldl.get(1)).get(k-1)).get("title"));
                                    }
                                    row.createCell(j).setCellValue(title);
                                }else if(j==7){
                                    if(!(ldl.get(j).toString().equals("null"))){
                                        row.createCell(j).setCellValue(((JSONObject) ((JSONArray) ldl.get(j)).get(0)).get("message").toString());
                                    }
                                }else if(j==9){
                                    if(((JSONArray) ldl.get(j)).size()>0){
                                        row.createCell(j).setCellValue(((JSONObject) ((JSONArray) ldl.get(j)).get(0)).get("title").toString());
                                    }
                                }else if(j==10){
                                    String chuangshu = "有";
                                    if(((JSONArray) ldl.get(10)).size()>0){
                                        JSONArray roomArray =  JSONArray.fromObject((((JSONObject)((JSONArray) ldl.get(10)).get(0)).get("beds")));
                                        chuangshu = chuangshu+roomArray.size()+"张床。";
                                        String type = "";
                                        for (int k = 1; k <roomArray.size()+1 ; k++) {
                                            type = type + "第"+k+"张床类型："+((JSONObject) roomArray.get(k-1)).get("type").toString()+"。    ";
                                        }
                                        row.createCell(j).setCellValue(chuangshu+type);
                                    }else{
                                        row.createCell(j).setCellValue(chuangshu+"0张床");
                                    }
                                }else if(j==16){
                                    row.createCell(j).setCellValue(((JSONObject) ((JSONArray) ldl.get(j)).get(0)).get("large").toString());
                                }else if(j==17){
                                    row.createCell(j).setCellValue(((JSONObject) ldl.get(17)).get("about").toString());
                                }else if(j==22){
                                    row.createCell(j).setCellValue(((JSONObject) ldl.get(22)).get("profile_pic_path").toString());
                                }else if(j==29){
                                    if(!ldl.get(29).equals(null)&&((JSONArray) ldl.get(j)).size()>0){
                                        String chuangshu = "有";
                                        JSONArray hezuofangdongArray =  ((JSONArray) ldl.get(29));
                                        chuangshu = chuangshu+hezuofangdongArray.size()+"位合作房东。";
                                        String type = "";
                                        for (int k = 1; k <hezuofangdongArray.size()+1 ; k++) {
                                            type = type + "第"+k+"位合作房东信息，姓名："+((JSONObject) hezuofangdongArray.get(k-1)).get("host_name").toString()+"，注册时间："+((JSONObject) hezuofangdongArray.get(k-1)).get("member_since").toString()+"。   ";
                                        }
                                        row.createCell(j).setCellValue(chuangshu+type);
                                    }else{
                                        row.createCell(j).setCellValue("无信息");
                                    }
                                }else if(j==30){
                                    if(!ldl.get(30).equals(null)) {
                                        row.createCell(j).setCellValue(((JSONObject) ldl.get(30)).get("summary").toString());
                                    }
                                }else if(j==35){
                                    if(!ldl.get(35).equals(null)&&((JSONArray) ldl.get(35)).size()>0) {
                                        row.createCell(j).setCellValue(((JSONObject) ((JSONArray) ldl.get(35)).get(0)).get("title").toString());
                                    }else{
                                        row.createCell(j).setCellValue("空");
                                    }
                                }else if(j==43){
                                    row.createCell(j).setCellValue(((JSONObject) ldl.get(43)).get("review_count").toString());
                                }else if(j==44){
                                    int commentSize = ((JSONArray) ldl.get(44)).size();
                                    String commonts = "评论详情：";
                                    for (int k=1;k<commentSize+1;k++){
                                        commonts=commonts+k+(((JSONObject) ((JSONArray) ldl.get(44)).get(k-1)).get("comments"));
                                    }
                                    row.createCell(j).setCellValue(commonts);
                                }else if(j==46){
                                    if(!ldl.get(46).equals(null)&&((JSONArray) ldl.get(j)).size()>0){
                                        String chuangshu = "有";
                                        JSONArray hezuofangdongArray =  ((JSONArray) ldl.get(46));
                                        chuangshu = chuangshu+hezuofangdongArray.size()+"条分享机制。";
                                        String type = "";
                                        for (int k = 1; k <hezuofangdongArray.size()+1 ; k++) {
                                            type = type + k+"："+((JSONObject) hezuofangdongArray.get(k-1)).get("url").toString()+"。  ";
                                        }
                                        row.createCell(j).setCellValue(chuangshu+type);
                                    }else{
                                        row.createCell(j).setCellValue("无信息");
                                    }
                                }else{
                                    row.createCell(j).setCellValue(ldl.get(j).toString());
                                }
                            }
                            lj++;
                        }
                    } catch (Exception e){
                        throw e;
                    }finally {
                        continue;
                    }
            }
            try{
                System.out.println("现在北京时间============"+CityNma+CityNma+CityNma+CityNma+CityNma+CityNma+"==========导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕导出完毕");
                FileSystemView fsv = FileSystemView.getFileSystemView();
                FileOutputStream outputStream = new FileOutputStream(new File(fsv.getHomeDirectory().toString() + "/"+CityNma+"airbnb数据" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls"));
                wb.write(outputStream);
                outputStream.flush();
                outputStream.close();
            }catch (IOException e){
               e.printStackTrace();
            }
        }
    }

}
